package com.mall.malllibrary.fragments.bottom

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.core.graphics.drawable.IconCompat.IconType
import com.joanzapata.iconify.widget.IconTextView
import com.mall.malllibrary.R
import com.mall.malllibrary.fragments.MallFragment
import me.yokeyword.fragmentation.ISupportFragment


// 抽象的方法用于统一管理一些公共的东西，不负责具体实现
abstract class BaseBottomFragment : MallFragment() , OnClickListener{

    //
    private val mTabBean = ArrayList<BottomTabBean>()

    // 每个tabbarItem对应的fragment
    val mItemFragments = ArrayList<BottomItemFragment>()

    // 使用顺序map绑定item和fragment
    private val mItems = LinkedHashMap<BottomTabBean, BottomItemFragment>()
    // 当前选中的item
    private var mCurrentFragment = 0
    // 当前选中的fragment
    private var mIndexFragment = 0
    // 选中的对应的颜色
    private var mCLickColor = Color.RED

    // 延迟加载底部tabbar
    private lateinit var mBottomBar : LinearLayout

    // 设置对应的tabbarItem所对应的数组
    abstract fun setItems(builder : ItemBuilder):LinkedHashMap<BottomTabBean, BottomItemFragment>

    // 设置视图
    override fun setLayout(): Any {
        return R.layout.fragment_bottom
    }

    // 抽象方法用于设置选中的fragment
    abstract fun setIndexFragment():Int

    // 使用注解实现设置颜色
    @ColorInt
    abstract fun setClickedColor():Int

    // 覆盖父类的oncreate方法来构建视图
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mIndexFragment = setIndexFragment()
        if (setClickedColor() != 0){
            mCLickColor = setClickedColor()
        }
        val builder = ItemBuilder.builder()
        val items = setItems(builder)
        mItems.putAll(items)

        for ((key, value) in mItems){
            mTabBean.add(key)
            mItemFragments.add(value)
        }
    }


    override fun onBindView(savedInstanceState: Bundle?, rootview: View) {
        mBottomBar = findView(R.id.bottomBar)
        val size = mItems.size
        for (i in 0 until size){
            layoutInflater.inflate(R.layout.bottom_item_icon_text, mBottomBar)
            val item = mBottomBar.getChildAt(i) as RelativeLayout

            // 为了正确的辨认点击事件
            item.tag = i
            item.setOnClickListener(this)
            val itemIcon = item.getChildAt(0) as IconTextView
            val itemTitle = item.getChildAt(1) as TextView

            // 初始化数据
            val bean = mTabBean[i]

            itemIcon.text  = bean.icon
            itemTitle.text = bean.title
            if(i == mIndexFragment){
                itemIcon.setTextColor(mCLickColor)
                itemTitle.setTextColor(mCLickColor)
            }
        }
        val fragment = mItemFragments.toTypedArray<ISupportFragment>()
        supportDelegate.loadMultipleRootFragment(R.id.bottomBar, mIndexFragment, *fragment)

    }
    // 重置颜色
    private fun resetColor(){

        val count = mBottomBar.childCount
        for (i in 0 until count){
            val item = mBottomBar.getChildAt(i) as RelativeLayout
            val itemIcon = item.getChildAt(0) as IconTextView
            val itemTitle = item.getChildAt(1) as TextView
            itemIcon.setTextColor(Color.RED)
            itemTitle.setTextColor(Color.RED)
        }

    }

    // 改变颜色
    private fun changeColor(tabIndex:Int){
        resetColor()

        val item = mBottomBar.getChildAt(tabIndex) as RelativeLayout
        val itemIcon = item.getChildAt(0) as IconTextView
        val itemTitle = item.getChildAt(1) as TextView
        itemIcon.setTextColor(Color.GRAY)
        itemTitle.setTextColor(Color.GRAY)
    }

    // 点击方法
    override fun onClick(p0: View?) {
        val tabIndex = p0?.tag as Int
        changeColor(tabIndex)
        // 展示和影藏内容部分的
        supportDelegate.showHideFragment(mItemFragments[tabIndex], mItemFragments[mCurrentFragment])

        mCurrentFragment = tabIndex

    }


}